'''
https://www.nowcoder.com/practice/7f8a8553ddbf4eaab749ec988726702b
'''
import sys


class Trie:
    MAXN = 150001
    tree = [[0 for _ in range(26)] for _ in range(MAXN)]
    ended = [0 for _ in range(MAXN)]
    passed = [0 for _ in range(MAXN)]

    def __init__(self):
        self.cnt = 1

    def insert(self, word):
        cur = 1
        Trie.passed[cur] += 1

        for char in word:
            path = ord(char) - ord('a')
            if not Trie.tree[cur][path]:
                self.cnt += 1
                Trie.tree[cur][path] = self.cnt
            cur = Trie.tree[cur][path]
            Trie.passed[cur] += 1
        Trie.ended[cur] += 1

    def search(self, word):
        cur = 1
        for char in word:
            path = ord(char) - ord('a')
            if not Trie.tree[cur][path]:
                return 0
            cur = Trie.tree[cur][path]
        return Trie.ended[cur]

    def startsWith(self, prefix):
        cur = 1
        for char in prefix:
            path = ord(char) - ord('a')
            if not Trie.tree[cur][path]:
                return 0
            cur = Trie.tree[cur][path]
        return Trie.passed[cur]

    def remove(self, word):
        time = self.search(word)
        if time == 0:
            return

        cur = 1
        Trie.passed[cur] -= 1
        for char in word:
            path = ord(char) - ord('a')
            child = Trie.tree[cur][path]
            Trie.passed[child] -= 1
            if Trie.passed[child] == 0:
                Trie.tree[cur][path] = 0
                return
            cur = child
        Trie.ended[cur] -= 1

trie = Trie()
n = int(sys.stdin.readline().strip())
for i in range(n):
    a = sys.stdin.readline().strip().split()
    opt = a[0]
    if opt == '1':
        trie.insert(a[1])
    elif opt == '2':
        trie.remove(a[1])
    elif opt == '3':
        times = trie.search(a[1])
        sys.stdout.write(('YES' if times > 0 else 'NO') + '\n')
    else:
        num = trie.startsWith(a[1])
        sys.stdout.write(str(num) + '\n')
